package com.example.crazy3d;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;

public class MainActivity extends AppCompatActivity {
    public InformationDictatorship InformationDictatorship_main;
    public PrintScavengers PrintScavengers_main;
    Button StopButton;
    Button PauseButton;
    ProgressBar PrintProgressBar;
    ProgressBar ConnectProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Logger.addLogAdapter(new AndroidLogAdapter());
        InformationDictatorship_main = new InformationDictatorship(MainActivity.this);
        PrintScavengers_main = new PrintScavengers(MainActivity.this);
        Button PrintButton = findViewById(R.id.button_print);
        StopButton = findViewById(R.id.Stop_button);
        PauseButton = findViewById(R.id.Pause_button);
        PrintProgressBar = findViewById(R.id.print_progressBar);
        ConnectProgressBar = new ProgressBar(this);
        PrintButton.setOnClickListener(v -> {
            FileUtils.RequestPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");//无类型限制
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, 1);
        });
        StopButton.setOnClickListener(v -> Logger.d("on StopButton"));
        StopButton.setEnabled(false);
        PauseButton.setOnClickListener(v -> Logger.d("on PauseButton"));
        PauseButton.setEnabled(false);
    }
    public final void SendMessageTo(int des, Message msg){
        switch (des) {
            case StatusUtils.InformationDictatorship:
                InformationDictatorship_main.SendMessage(StatusUtils.InformationDictatorship, msg);
                break;
            case StatusUtils.PrintScavengers:

            default:
                break;
        }
    }
    public final Handler MainHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case StatusUtils.ErrorMessage:
                    Logger.d("StatusUtils ErrorMessage -> " + msg.obj);
                    break;
                case StatusUtils.PrintingPipeline:
                    /* 读文件数据 */
                    switch (msg.arg1) {
                        case StatusUtils.MainActivity:
                            /* main的数据 */
                            PrintProgressBar.setMax(100);
                            PrintProgressBar.setProgress((int)msg.obj);
                            break;
                        case StatusUtils.InformationDictatorship:
                            SendMessageTo(StatusUtils.InformationDictatorship, msg);
                            break;
                        case StatusUtils.PrintScavengers:
                            SendMessageTo(StatusUtils.PrintScavengers, msg);
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
        }
    };
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);

          if (resultCode == Activity.RESULT_OK) {
              if (data != null) {
                  String FilePath = FileUtils.getPath(MainActivity.this,data.getData());

                  Logger.d("获取的文件路径" + FilePath);
                  Toast.makeText(MainActivity.this, FilePath, Toast.LENGTH_SHORT).show();

                  new AlertDialog.Builder(this)
                          .setTitle("要开始么")
                          .setIcon(R.mipmap.ic_launcher_round)
                          .setMessage(data.getData().getPath())
                          .setPositiveButton("确定"
                                  , (dialog, which) -> {
                                      Message Path_msg = Message.obtain();
                                      Path_msg.what = StatusUtils.PrintScavengers;
                                      Path_msg.obj = FilePath;
                                      PrintScavengers_main.PrintScavengers_Handler.sendMessage(Path_msg);
                                      /* 目前只能执行一个 */
                                      findViewById(R.id.button_print).setEnabled(false);
                                      PauseButton.setEnabled(true);
                                      StopButton.setEnabled(true);
                                  })
                          .setNegativeButton("取消"
                                  , (dialog, which) -> Toast.makeText(MainActivity.this, "取消了操作" + data.getData().getPath(), Toast.LENGTH_SHORT).show())
                          .create()
                          .show();
              }
          }
      }
}